package hos.base.dialog;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.android.material.bottomsheet.BottomSheetDialog;

import hos.base.activity.BaseActivity;
import hos.base.view.IFragmentActivityX;
import hos.thread.IThread;

/**
 * <p>Title: BaseBottomSheetDialog </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2024-05-13 10:17
 */
public abstract class BaseBottomSheetDialog extends BottomSheetDialog implements IThread, IFragmentActivityX<BaseActivity> {

    private View contentView;

    public BaseBottomSheetDialog(@NonNull Context context) {
        this(context, 0);
    }

    public BaseBottomSheetDialog(@NonNull Context context, int theme) {
        super(context, theme);
        init();
    }

    protected BaseBottomSheetDialog(@NonNull Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init();
    }

    private void init() {
        LayoutInflater inflater = getLayoutInflater();
        contentView = onCreateView(inflater, null, null);
        setContentView(contentView);
        onViewShowed(contentView);
    }

    public View getContentView() {
        return contentView;
    }

    @NonNull
    public abstract View onCreateView(@NonNull LayoutInflater inflater,
                                      @Nullable ViewGroup container,
                                      @Nullable Bundle savedInstanceState);

    protected void applyBackground(View view) {
        int padding12 = DialogUtils.dp2px(12);
        int padding8 = DialogUtils.dp2px(8);
        view.setBackground(DialogUtils.getGradientDrawableBottom());
        view.setPadding(padding12, padding8, padding12, padding8);
    }


    @Override
    public void show() {
        postOnMain(() -> {
            if (!isShowing()) {
                BaseBottomSheetDialog.super.show();
            }
        });
    }

    @Override
    public void dismiss() {
        postOnMain(() -> {
            if (isShowing()) {
                BaseBottomSheetDialog.super.dismiss();
            }
        });
    }


}
